﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Search.BLL
{
    public class CommonService : BaseService, ICommonService
    {
        private ICommonDAO commonDAO = null;

        public CommonService(ICommonDAO commonDAO)
        {
            this.commonDAO = this.generator.CreateInterfaceProxyWithTarget<ICommonDAO>(commonDAO, new IInterceptor[] { new LogInterceptor() });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbCode"></param>
        /// <returns></returns>
        public SYS_CMS_DATABASE GetDatabaseByCode(string dbCode)
        {
            SYS_CMS_DATABASE result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("DatabaseCode", OperatorType.Equal, dbCode);
                IList<SYS_CMS_DATABASE> list = commonDAO.Get<SYS_CMS_DATABASE>(exp.Compile());
                if (list != null && list.Count > 0)
                {
                    result = list[0];
                    result.DatabaseCode = StringHelper.RemoveHighlightFlag(result.DatabaseCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SYS_CMS_DATABASE GetDatabaseByID(int id)
        {
            SYS_CMS_DATABASE result = null;
            try
            {
                ExpressionCombine<SYS_CMS_DATABASE> exp = new ExpressionCombine<SYS_CMS_DATABASE>();
                exp.And("SysID", OperatorType.Equal, id);
                IList<SYS_CMS_DATABASE> list = commonDAO.Get<SYS_CMS_DATABASE>(exp.Compile());
                if (list != null && list.Count > 0)
                {
                    result = list[0];
                    result.DatabaseCode = StringHelper.RemoveHighlightFlag(result.DatabaseCode);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }

            return result;
        }
    }
}
